package cz.muni.fi.pb168.eis_web;

import javax.servlet.http.HttpSession;

/**
 *
 * @author Me
 */
public class UserLogin
{
    private HttpSession session;
    
    private Authenticator authenticator;
    
    public UserLogin(HttpSession session, Authenticator authenticator)
    {
        this.session =  session;
        
        this.authenticator = authenticator;
    }
    
    public boolean login(String userName, String userPassword)
    {
        if(authenticator.authenticate(new String[] {userName, userPassword}))
        {
            session.setAttribute("userName", userName);
            
            session.setAttribute("isLoggedIn", true);
            
            return true;
        }
        
        return false;
    }
    
    public void logout()
    {
        session.setAttribute("isLoggedIn", false);
    }
    
    public boolean isLoggedIn()
    {
        Object o = session.getAttribute("isLoggedIn");
        
        try
        {
            return  (o != null && ((Boolean) o).booleanValue());
        }
        catch(Exception e)
        {
            return false;
        }
    }
}
